package cn.com.xy.sms.sdk.ui.popu.widget;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.com.xy.sms.sdk.constant.Constant;
import cn.com.xy.sms.sdk.log.LogManager;
import cn.com.xy.sms.sdk.ui.R;
import cn.com.xy.sms.sdk.ui.popu.util.ContentUtil;
import cn.com.xy.sms.sdk.ui.popu.util.SimpleButtonUtil;
import cn.com.xy.sms.sdk.util.JsonUtil;
import cn.com.xy.sms.sdk.util.StringUtils;

/**
 * Created by helen on 2017/3/23.
 */

public class SimpleBottomTable extends LinearLayout {

    private final String TAG = "SimpleBottomTable";
    private Activity mContext;
    private List<ViewHolder> mViewHolders;
    private boolean mDisLogo;
    private HashMap<String, Object> mExtend;


    public SimpleBottomTable(Context context, AttributeSet attrs) {
        super(context, attrs);
        //this.mContext = (Activity) context;
    }

    public void setDisLogo(boolean disLogo){
        this.mDisLogo = disLogo;
    }
    public void setExtend(HashMap<String, Object> extend) {
        this.mExtend = extend;
    }

    public void setContentList(List<JSONArray> arrays, int dataSize){
        try{
            if (dataSize == 0) {
                this.setVisibility(View.GONE);
                if(mViewHolders!=null){
                    int size = mViewHolders.size();
                    for(int i=0;i<size;i++){
                        mViewHolders.get(i).setVisible(View.GONE);
                    }
                }
                return;
            }

            this.setVisibility(View.VISIBLE);
            final List<ViewHolder> holderList = mViewHolders;
            if (holderList != null) {
                int holderSize = holderList.size();
                int diffSize = holderSize - dataSize;
                ViewHolder tempHolder=null;
                if (diffSize > 0) {
                    for (int i = 0; i < holderSize; i++) {
                        tempHolder = holderList.get(i);
                        if (i < dataSize) {
                            tempHolder.setVisible(View.VISIBLE);
                            tempHolder.bindData(i, arrays);
                        } else {
                            tempHolder.setVisible(View.GONE);
                        }
                    }
                } else {
                    for (int i = 0; i < dataSize; i++) {
                        if (i < holderSize) {
                            tempHolder = holderList.get(i);
                            tempHolder.setVisible(View.VISIBLE);
                            tempHolder.bindData(i, arrays);
                        } else {
                            getHolder(i, arrays);
                        }
                    }
                }
                return;
            }
            mViewHolders = new ArrayList<ViewHolder>();
            for (int i = 0; i < dataSize; i++) {
                getHolder(i, arrays);
            }
        }catch (Throwable ex){
            LogManager.e(TAG, ex.getMessage());
        }
    }

    public void getHolder(int pos, List<JSONArray> jsonArray){
        try{
            ViewHolder viewHolder = new ViewHolder();
            viewHolder.bindData(pos, jsonArray);
            this.addView(viewHolder.mHolderView);
            mViewHolders.add(viewHolder);
        }catch (Throwable ex){
            LogManager.e(TAG, ex.getMessage());
        }
    }

    private class ViewHolder {
        private View mHolderView;
        private ImageView mImageViewIcon;
        private TextView mBtnNameTv;

        public ViewHolder(){
            mHolderView = View.inflate(Constant.getContext(), R.layout.duoqu_simple_bubble_bottom_two, null);
            mImageViewIcon = (ImageView) mHolderView.findViewById(R.id.duoqu_simple_view_icon);
            mBtnNameTv = (TextView) mHolderView.findViewById(R.id.duoqu_simple_view_tv);
        }

        public void bindData(int pos, List<JSONArray> itemArrList){
            if(itemArrList == null || itemArrList.size() <= 0) {
                return;
            }
            try{
                JSONArray itemArray = itemArrList.get(pos);
                if(itemArray == null || itemArray.length() <= 0){
                    return;
                }
                JSONObject actionMap = itemArray.optJSONObject(0);
                if(actionMap == null){
                    return;
                }
                int size = itemArrList.size();
                setLayBg(pos, size);
                String btnName = ContentUtil.getBtnName(actionMap);
                if (!StringUtils.isNull(btnName)) {
                    mBtnNameTv.setText(btnName);
                    int resLogoId = SimpleButtonUtil.bindButtonData(mBtnNameTv, (String) JsonUtil.getValueFromJsonObject(actionMap, "action"), false, false);
                    if(resLogoId != -1){
                        mImageViewIcon.setBackgroundResource(resLogoId);
                    }
                }

                bindListener(itemArray, actionMap);
            }catch (Throwable ex){
                LogManager.e(TAG, ex.getMessage());
            }
        }

        private void bindListener(final JSONObject actionMap){
            mHolderView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleButtonUtil.doAction(mContext, actionMap, mExtend);
                }
            });
        }

        public void bindListener(final JSONArray itemArray, final JSONObject actionMap){
            mHolderView.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    SimpleButtonUtil.btnClickMoreData(mContext, itemArray, actionMap, null);
                }
            });
        }

        public void setVisible(int visible){
            if(mHolderView != null){
                mHolderView.setVisibility(visible);
            }
        }

        private void setLayBg(int pos, int size){
            if(size == 1 && pos == 0){
                mHolderView.setBackgroundResource(R.drawable.duoqu_simple_btn_down_bg);
            } else if(size > 1 && pos == 0){
                mHolderView.setBackgroundResource(R.drawable.duoqu_simple_top_btn_down_bg);
            } else if(size > 2 && pos + 1 != size){
                mHolderView.setBackgroundResource(R.drawable.duoqu_simple_mid_btn_down_bg);
            } else if(size >= 2 && pos + 1 == size){
                mHolderView.setBackgroundResource(R.drawable.duoqu_simple_bottom_btn_down_bg);
            }
        }
    }
}
